package com.atguigu.sparkcore.demo

import com.atguigu.sparkcore.util.MySparkContextUtil
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
  * WordCount类
  */
object WordCount {

  def main(args: Array[String]): Unit = {

    // 获取sparkContext
    val sc = MySparkContextUtil.get(args)

    // 2、创建rdd
    val rdd: RDD[String] = sc.textFile("hdfs://hadoop101:9000/input")

    // 3、操作rdd
    val data: RDD[(String, Int)] = rdd.flatMap(_.split("\t")).map((_, 1)).reduceByKey(_ + _)

    // 4、对rdd进行执行算子
    val result: Array[(String, Int)] = data.collect()
    result.foreach(println)

    // 5、关闭资源
    sc.stop()
  }
}
